package info.fernandoguillen.javadb2.tests;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;


/**
 * <p>
 * </p>
 * 
 * @internal_author fguillen.mail@gmail.com
 * @date 11/04/2008
 *
 */
public class TestJTOpenMain {
	private static final Logger log = 
		Logger.getLogger( TestJTOpenMain.class );

	/**
	 * <p>
	 * 
	 * </p>
	 * @date: 11/04/2008
	 * @author: fguillen.mail@gmail.com
	 *
	 * @param args
	 * @throws SQLException 
	 */
	public static void main(String[] args) throws SQLException {
		if( log.isDebugEnabled() ) {
			log.debug( "main:INI" );
			log.debug( "args:" + args );
		}
		
		if( args.length != 3 ){
			log.error( "use it with three arguments: ipServer userName userPassword" );
			System.exit(1);
		}
		
		String ipServer = args[0];
		String username = args[1];
		String password = args[2];
		String database = "";
		
		if( log.isDebugEnabled() ) {
			log.debug( "ipServer:" + ipServer );
			log.debug( "username:" + username );
			log.debug( "password:" + password );
			log.debug( "database:" + database );
		}
		
		TestJTOpenMain main = new TestJTOpenMain();
		
		//
		// abrimos la conexion
		//
		Connection connection = 
			main.getConnectionAS400Driver(
				username, 
				password, 
				ipServer, 
				database
			);
		
		//
		// insertamos un registro
		//
		main.insertARegister(
			connection, 
			"texto de prueba"
		);
		
		//
		// insertamos un registro vía PreparedStatement
		//
		main.insertByPreparedStatement(
			connection, 
			"texto de prueba de PreparedStatement"
		);
		
		
		if( log.isDebugEnabled() ) {
			log.debug( "main:END" );
		}
		
	}
	
	public void insertARegister(
		Connection connection,
		String text
	) throws SQLException {
		if( log.isDebugEnabled() ) {
			log.debug( "insertARegister:INI" );
			log.debug( "connection:" + connection );
			log.debug( "text:" + text );
		}
		
		String sql = 
			"insert into MESSAGES ( MESSAGE_ID, MESSAGE_TEXT ) values ( default, '" + text + "' )";
		
		if( log.isDebugEnabled() ) {
			log.debug( "sql:" + sql );
		}
		
		Statement s = connection.createStatement();
		s.execute( sql );
		s.close();
		
		if( log.isDebugEnabled() ) {
			log.debug( "insertARegister:END" );
		}
		
	}
	
	public void insertByPreparedStatement(
		Connection connection,
		String text
	) throws SQLException{
		if( log.isDebugEnabled() ) {
			log.debug( "insertByPreparedStatement:INI" );
			log.debug( "connection:" + connection );
			log.debug( "text:" + text );
		}
		
		String sql = 
			"insert into MESSAGES ( MESSAGE_ID, MESSAGE_TEXT ) values ( default, ? )";
		
		if( log.isDebugEnabled() ) {
			log.debug( "sql:" + sql );
		}
		
		PreparedStatement ps = connection.prepareStatement(sql);
		ps.setString( 1, text );
		ps.executeUpdate();
		ps.close();
		
		if( log.isDebugEnabled() ) {
			log.debug( "insertByPreparedStatement:END" );
		}
	}
	
	public Connection getConnectionAS400Driver(
		String username, 
		String password,
		String ipServer,
		String Database
	) throws SQLException {
		if( log.isDebugEnabled() ) {
			log.debug( "getConnectionAS400Driver:INI" );
			log.debug( "username:" + username );
			log.debug( "password:" + password );
			log.debug( "Database:" + Database );
		}

		DriverManager.registerDriver(new com.ibm.as400.access.AS400JDBCDriver());
 
		Connection connection = null;
		String urlConnection = "jdbc:as400://" + ipServer + "/" + Database;
		
		log.debug( "urlConnection:" + urlConnection );
		connection = DriverManager.getConnection( urlConnection, username, password );
	
		if( log.isDebugEnabled() ) {
			log.debug( "connection:" + connection );
			log.debug( "getConnectionAS400Driver:END" );
		}
		
        return connection;
	}
}
